package com.forezp.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.forezp.dao.DeviceMapper;
import com.forezp.service.DeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.bean.Device;

@Service
public class DeviceServiceImpl implements DeviceService {

	@Autowired
	private DeviceMapper deviceMapper;

	@Override
	public PageInfo<Device> getPage(Device device, Integer pageNumber, Integer pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		List<Device> deviceList = deviceMapper.getList(device);
		return new PageInfo<Device>(deviceList);
	}

	@Override
	public void deviceOnLine(Device device) {
		if (StringUtils.isNotEmpty(device.getSerialNumber())) {
			Device dev = deviceMapper.getDeviceBySerialNumber(device.getSerialNumber());
			if (dev != null) {
				dev.setConnectionStatus(device.getConnectionStatus());
				dev.setLastContact(device.getLastContact());
				dev.setSerialNumber(device.getSerialNumber());
				dev.setIpAddr(device.getIpAddr());
				deviceMapper.updateById(dev);
			} else {
				device.setCreateDate(new Date());
				deviceMapper.insertDevice(device);
			}
		}
	}

	@Override
	public Integer deviceUpdate(Device device) {
		if(device.getId()!=null){
			return deviceMapper.updateById(device);
		}
		return null;	
	}

	@Override
	public Integer registerDevice(Integer[] ids, Integer typeId) {
		deviceMapper.deviceRegisters(typeId,ids);
		return null;
	}

}
